from flask import Flask, request, url_for, session, request, render_template, redirect

app = Flask(__name__)
# 设置应用的密钥，用于加密会话的数据，确保会话的安全
app.secret_key = "rensheng"


def is_admin():
    return session.get("username") == "admin"


@app.route("/admin")
def admin():
    if not is_admin():
        return redirect(url_for("login"))
    return "正常显示管理员的管理页"


@app.route("/login", methods=["GET", "POST"])
def login():
    if request.method == "POST":
        username = request.form["username"]
        password = request.form["password"]
        if username == "admin" and password == "admin123":
            # 把登录用户名写入session会话
            session["username"] = username
            return redirect(url_for("admin"))
        return "登录失败,请重试"
    else:
        return render_template("login.html", title="登录页面")


if __name__ == "__main__":
    app.run()
